import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener; //import java.awt.image.BufferedImage;

import javax.swing.JFrame;
import javax.swing.JPanel;

public class LaserHack extends JPanel implements KeyListener, Runnable
{
	private static final long serialVersionUID = 1L;

	private FontHandler console;
	private Map level;
	private static int f_height, f_width; // frame height and width
	long dirty_time; // This is used to accrue 'dirty time' until there's so
						// much that it needs to be cleaned up.

	boolean running;
	Thing itemlist[] =
	{ new Thing(4, 5, 2, "pistol"), new Thing(15, 5, 3, "red keycard"),
			new Thing(7, 4, 4, "helmet"), new Thing(3, 6, 5, "medkit"),
			new Thing(13, 3, 6, "alien egg"), new Thing(7, 27, 7, "wrench"),
			new Thing(15, 15, 8, "ugly boots") };

	public static void main(String[] args)
	{
		JFrame frame = new JFrame("Placeholder name");
		DOM dom = new DOM("config.dat");
		f_width = dom.getValue("width");
		f_height = dom.getValue("height");
		frame.setSize(f_width, f_height);
		LaserHack LH = new LaserHack();
		frame.add(LH);
		frame.setVisible(true);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		LH.requestFocus();
		frame.setResizable(false);
	}

	private LaserHack()
	{
		level = new Map("map.dat");
		console = FontHandler.getInstance();

		addKeyListener(this);
		Thread t = new Thread(this);
		t.start();
	}

	public void paintComponent(Graphics g)
	{
		super.paintComponent(g);
		g.setColor(Color.BLACK);
		g.fillRect(0, 0, f_width, f_height);
		level.draw(g);
		console.draw(g);
	}

	public void keyPressed(KeyEvent e)
	{
		switch ( e.getKeyCode() )
		{
		case KeyEvent.VK_UP:
			if (e.isShiftDown() == false)
			{
				level.movePlayer(Map.NORTH);
			}
			else
			{
				level.turnPlayer(Map.NORTH);
				console.addLine("Facing north.");
			}
			// font.addLine("UP");
			forceUpdate();
			break;
		case KeyEvent.VK_RIGHT:
			if (e.isShiftDown() == false)
			{
				level.movePlayer(Map.EAST);
			}
			else
			{
				level.turnPlayer(Map.EAST);
				console.addLine("Facing east.");
			}
			forceUpdate();
			break;
		case KeyEvent.VK_DOWN:
			if (e.isShiftDown() == false)
			{
				level.movePlayer(Map.SOUTH);
			}
			else
			{
				level.turnPlayer(Map.SOUTH);
				console.addLine("Facing south.");
			}
			forceUpdate();
			break;
		case KeyEvent.VK_LEFT:
			if (e.isShiftDown() == false)
			{
				level.movePlayer(Map.WEST);
			}
			else
			{
				level.turnPlayer(Map.WEST);
				console.addLine("Facing west.");
			}
			forceUpdate();
			break;
		case KeyEvent.VK_S:
			//level.drawScent();
			level.showScent = !level.showScent;
			break;
		case KeyEvent.VK_Z:
			// This activates the tile in front of the player.
			// TODO: Clean this up. Player should be a private member of level
			level.doAction(level.player.moveForward());
			forceUpdate();
			break;
		case KeyEvent.VK_ESCAPE:
			System.exit(0);
			// TODO eventually there will need to be a 'do you want to quit'
			// confirmation message.
			break;
		}
		// if (p.x != 0) update(1000);
	}

	public void keyTyped(KeyEvent e)
	{
	}

	public void keyReleased(KeyEvent e)
	{
	}

	private void update(int ms)
	{
		repaint();
		dirty_time += ms;
		if (dirty_time >= 1000)
		{
			dirty_time = 0;
			// Call enemy routines
			level.spreadScent();
			level.moveMobs();
		}
	}

	@Override
	public void run()
	{
		running = true;
		long now, then = System.currentTimeMillis(), passed;

		while (running)
		{
			now = System.currentTimeMillis();
			passed = now - then;
			then = now;
			update((int) passed);
			repaint();
			try
			{
				Thread.sleep(10);
			}
			catch (Exception e)
			{
			}

		}
	}

	private void forceUpdate()
	{
		dirty_time += 1000;

	}

}